package xor.droid.nav;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;

public class GLRenderer implements Renderer {

	private GLModel tri;

	public GLRenderer() {
		tri = new GLModel();
	}

	// When renderer gets created
	public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {

		// Performance boost
		gl.glDisable(GL10.GL_DITHER);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
		// ---
		// gl.glClearColor(.8f, 0f, .2f, 1f);
		gl.glClearDepthf(1f);

	}

	// will be refreshed over and over
	public void onDrawFrame(GL10 gl) {
		gl.glDisable(GL10.GL_DITHER);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		// tell the camera where to look -10 ist der zoom away
		GLU.gluLookAt(gl, 0, 0, -3, 0, 0, 0, 0, 2f, 0);
		// draws the triangle -> still we need to find it
		tri.draw(gl);
	}

	// Called when the surface changed size.
	// Called after the surface is created
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);
		// ratio same in portrait und landscape mode
		float ratio = (float) width / height;
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		// how far in the Z direction do we want our triangle to be seen
		// left bottom top value ratio wegen landscape -> portrait
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);

	}

}
